package 剑指Offer.easy;

import java.util.ArrayList;
import java.util.List;

public class 和为s的连续正序列 {
    //滑动窗口
    class Solution {
        public int[][] findContinuousSequence(int target) {
            List<int[]> res = new ArrayList<>();
            //i为窗口左界，j为窗口右界，s为窗口值的总合
            int i = 1, j = 2, s = 3;
            while (i < j) {
                if (s == target) {
                    //返回当前窗口结果
                    int[] tmp = new int[j - i + 1];
                    for (int k = i; k <= j; k++) {
                        tmp[k - i] = k;
                    }
                    res.add(tmp);
                    //左界右移
                    s -= i++;
                } else if (s < target) {
                    //右界右移
                    s += ++j;
                } else {
                    //左界右移
                    s -= i++;
                }
            }
            return res.toArray(new int[res.size()][]);
        }
    }
}
